package cn.tedu.mintmall.controller;

import cn.tedu.mintmall.dto.OrderAddDTO;
import cn.tedu.mintmall.dto.OrderListTimeDTO;
import cn.tedu.mintmall.dto.OrderStateUpdateDTO;
import cn.tedu.mintmall.restful.JsonPage;
import cn.tedu.mintmall.restful.JsonResult;
import cn.tedu.mintmall.service.IOrderService;
import cn.tedu.mintmall.vo.OrderAddVO;
import cn.tedu.mintmall.vo.OrderListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
@Api(tags = "订单功能")
public class OrderController {
    @Autowired
    private IOrderService orderService;

    @PostMapping("/add")
    @ApiOperation("生成订单的方法")
    public JsonResult<OrderAddVO> addOrder(@Validated OrderAddDTO orderAddDTO) {
        OrderAddVO orderAddVO = orderService.addOrder(orderAddDTO);
        return JsonResult.ok("返回成功");
    }


    @GetMapping("/list")
    @ApiOperation("分页查询当前登录用户指定时间范围的订单信息")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult<JsonPage<OrderListVO>> listUserOrders(OrderListTimeDTO orderListTimeDTO){
        JsonPage<OrderListVO> list = orderService.listOrdersBetweenTimes(orderListTimeDTO);
        return JsonResult.ok("成功",list);
    }

    @PostMapping("/update/state")
    @ApiOperation("修改订单状态的方法")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult updateOrderState (OrderStateUpdateDTO orderStateUpdateDTO){
        orderService.updateOrderState(orderStateUpdateDTO);
        return JsonResult.ok();
    }
}